package com.xhui.vimarket.zkplus.zul.attr.bus.impl;

import java.util.HashMap;

import lombok.Getter;
import lombok.Setter;
com.xhui.vimarket.zkplusui.vimarket.zkplus.zul.attrcom.xhui.vimarket.zkplusrt com.xhui.vimarket.zkplus.zulcom.xhui.vimarket.zkplusus;
import com.xhui.vimarket.zcom.xhui.vimarket.zkplusbus.AttrFun;
import com.xhui.vimarket.zkplus.zul.attr.bus.AttrProc;

/**
 * .
 * 
 * @版权：福富软件 版权所有 (c) 2011
 * @author wuyx
 * @version Revision 1.0.0
 * @see:
 * @创建日期：2012-1-4
 * @功能说明：
 * 
 */
public class AttrBusImpl implements AttrBus {
    
    /**
     * 事件处理监听器列表.
     */
    private HashMap<String, HashMap<AttrProc, AttrProc>> procLisList = new HashMap<String, HashMap<AttrProc, AttrProc>>();
    
    /**
     * 方法对象列表.
     */
    private HashMap<String, AttrFun>                     funLisList  = new HashMap<String, AttrFun>();
    
    /**
     * 控总线.
     */
    public static AttrBus                                nullAttrBus = new AttrBusImpl();
    
    /**
     * .
     */
    @Getter
    @Setter
    private AttrExt                                      attrExt;
    
    @Override
    public void addFunListener(String funName, AttrFun attrFun) {
        this.funLisList.put(funName, attrFun);
    }
    
    @Override
    public void addProcListener(String procName, AttrProc attrProc) {
        HashMap<AttrProc, AttrProc> theProcList = this.getProcListener(procName);
        if (theProcList == null) {
            theProcList = new HashMap<AttrProc, AttrProc>();
            theProcList.put(attrProc, attrProc);
            this.procLisList.put(procName, theProcList);
        } else {
            theProcList.put(attrProc, attrProc);
        }
    }
    
    @Override
    public void fireProcListener(String procName, Object obj) {
        HashMap<AttrProc, AttrProc> theProcList = this.getProcListener(procName);
        if (theProcList != null && theProcList.size() > 0) {
            for (AttrProc proc : theProcList.keySet()) {
                proc.onEvent(obj, this.getAttrExt());
            }
        }
    }
    
    @Override
    public AttrFun getFunListener(String funName) {
        return this.funLisList.get(funName);
    }
    
    @Override
    public void removeProcListener(String procName) {
        this.procLisList.remove(procName);
    }
    
    @Override
    public void removeProcListener(String procName, AttrProc attrProc) {
        HashMap<AttrProc, AttrProc> theProdList = this.getProcListener(procName);
        if (theProdList != null) {
            theProdList.remove(attrProc);
        }
    }
    
    /**
     * getProcListener.
     * 
     * @param procName String
     * @return ArrayList<AttrProc>
     * @author wuyx 
     * 2012-1-4 wuyx 
     */
    private HashMap<AttrProc, AttrProc> getProcListener(String procName) {
        HashMap<AttrProc, AttrProc> obj = this.procLisList.get(procName);
        if (obj != null) {
            return obj;
        }
        return null;
    }
    
}
